package com.silverbox.waimai.service.goodscart.impl;

import java.util.List;

import com.silverbox.core.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.silverbox.waimai.entity.GoodsCart;
import com.silverbox.waimai.entity.extend.GoodsCartAndDetail;
import com.silverbox.waimai.entity.extend.GoodsCartExtend;
import com.silverbox.waimai.mapper.GoodsCartMapper;
import com.silverbox.waimai.service.goodscart.GoodsCartService;

@Service
public class GoodsCartServiceImpl implements GoodsCartService {

	@Autowired
	private GoodsCartMapper goodsCartMapper;

	@Override
	public List<GoodsCartAndDetail> getGoodsCart(GoodsCartExtend goodsCartExtend) {
		return goodsCartMapper.getGoodsCartAndDetail(goodsCartExtend);
	}

	@Override
	public void addGoodsCart(GoodsCart goodsCart) {
		goodsCartMapper.insertSelective(goodsCart);
	}

	@Override
	public int updateGoodsCart(GoodsCart goodsCart) {
		return goodsCartMapper.updateByPrimaryKeySelective(goodsCart);
	}

	@Override
	public int removeGoodsCart(GoodsCart goodsCart) {
		return goodsCartMapper.removeGoodsCart(goodsCart);
	}

	@Override
	public GoodsCart getSameGoodsProp(GoodsCartExtend goodsCartExtend) {
		List<GoodsCart> carts = goodsCartMapper.getSameGoodsProp(goodsCartExtend);
		if(CollectionUtils.isEmpty(carts)){
			return null;
		}else{
			return carts.get(0);
		}
	}

	@Override
	public int getSameGoodsDiffPropNum(GoodsCartExtend goodsCartExtend) {
		return goodsCartMapper.getSameGoodsDiffPropNum(goodsCartExtend);
	}

	@Override
	public int deleteGoodsCartById(Integer id) {
		return goodsCartMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteGoodsCartForPack(GoodsCartExtend goodsCart) {
		return goodsCartMapper.deleteGoodsCartForPack(goodsCart);
	}

	@Override
	public int updateGoodsCartForPack(GoodsCartExtend goodsCart) {
		return goodsCartMapper.updateGoodsCartForPack(goodsCart);
	}

	@Override
	public int getSameGoodsCartPackCount(GoodsCartExtend goodsCart) {
		return goodsCartMapper.getSameGoodsCartPackCount(goodsCart);
	}

	@Override
	public GoodsCartAndDetail getGoodsCartById(Integer id) {
		return goodsCartMapper.getGoodsCartById(id);
	}

	@Override
	public GoodsCart getGoodsCartByRemarkCommit(GoodsCartExtend goodsCart) {
		return goodsCartMapper.getGoodsCartByRemarkCommit(goodsCart);
	}

	@Override
	public int updateGoodsCartForTableNo(List<Integer> goodsCartId, String tableName) {
		return goodsCartMapper.updateGoodsCartForTableNo(goodsCartId,tableName);
	}

	@Override
	public int deleteGoodsCartByTableName(GoodsCartExtend goodsCart) {
		return goodsCartMapper.deleteGoodsCartByTableName(goodsCart);
	}

	@Override
	public int updateGoodsCartForMustGoods(GoodsCartExtend goodsCartExtend, List<GoodsCartAndDetail> goodsCartList) {
		if(goodsCartExtend!=null){
			//删除必选菜
			goodsCartMapper.deleteGoodsCartForPack(goodsCartExtend);
		}
		//新增必选菜(新增必选菜商品没有明细暂时不用插入goods_detail表)
		int insertCount = 0;
		if(!CollectionUtils.isEmpty(goodsCartList)){
			insertCount = goodsCartMapper.insertSelectiveBatch(goodsCartList);
		}
		return insertCount;
	}

	@Override
	public List<GoodsCart> getDeletedNeedGoodsCart(GoodsCartExtend goodsCartExtend) {
		return goodsCartMapper.getDeletedNeedGoodsCart(goodsCartExtend);
	}

	@Override
	public int removeDeletedNeedGoodsCart(GoodsCartExtend goodsCartExtend) {
		return goodsCartMapper.removeDeletedNeedGoodsCart(goodsCartExtend);
	}

	@Override
	public void updateNeedGoods(GoodsCart goodsCart) {
		goodsCartMapper.updateNeedGoods(goodsCart);
	}

    @Override
    public void deleteMyGoodsCart(Integer shopId, String buyerId) {
        String systemBuyerId = "system_"+buyerId;
        goodsCartMapper.deleteGoodsCartDetails(buyerId,shopId);
        goodsCartMapper.deleteGoodsCart(buyerId,systemBuyerId,shopId);
    }

    @Override
    public void deleteMyGoodsCartByTableNo(Integer shopId, String tableNo) {
        GoodsCartExtend extend = new GoodsCartExtend();
        extend.setStartDate(Utils.getYesterdayStartTime());
        extend.setEndDate(Utils.getTodayEndTime());
        extend.setStoreId(shopId);
        extend.setTableNo(tableNo);
        goodsCartMapper.deleteGoodsCartDetailsByTableNo(extend);
        goodsCartMapper.deleteGoodsCartByTableNo(extend);
    }
}
